<rd-header>
  <rd-header-title title="Users">
    <a data-toggle="tooltip" title="Refresh" ui-sref="users" ui-sref-opts="{reload: true}">
      <i class="fa fa-refresh" aria-hidden="true"></i>
    </a>
    <i id="loadUsersSpinner" class="fa fa-cog fa-spin" style="margin-left: 5px;"></i>
  </rd-header-title>
  <rd-header-content>User management</rd-header-content>
</rd-header>

<div class="row">
  <div class="col-lg-12 col-md-12 col-xs-12">
    <rd-widget>
      <rd-widget-header icon="fa-plus" title="Add a new user">
      </rd-widget-header>
      <rd-widget-body>
        <form class="form-horizontal">
          <!-- name-input -->
          <div class="form-group">
            <label for="username" class="col-sm-2 control-label text-left">Username</label>
            <div class="col-sm-8">
              <div class="input-group">
                <input type="text" class="form-control" id="username" ng-model="formValues.Username" ng-change="checkUsernameValidity()" placeholder="e.g. jdoe">
                <span class="input-group-addon"><i ng-class="{true: 'fa fa-check green-icon', false: 'fa fa-times red-icon'}[state.validUsername]" aria-hidden="true"></i></span>
              </div>
            </div>
          </div>
          <!-- !name-input -->
          <!-- new-password-input -->
          <div class="form-group">
            <label for="password" class="col-sm-2 control-label text-left">Password</label>
            <div class="col-sm-8">
              <div class="input-group">
                <span class="input-group-addon"><i class="fa fa-lock" aria-hidden="true"></i></span>
                <input type="password" class="form-control" ng-model="formValues.Password" id="password">
              </div>
            </div>
          </div>
          <!-- !new-password-input -->
          <!-- confirm-password-input -->
          <div class="form-group">
            <label for="confirm_password" class="col-sm-2 control-label text-left">Confirm password</label>
            <div class="col-sm-8">
              <div class="input-group">
                <span class="input-group-addon"><i class="fa fa-lock" aria-hidden="true"></i></span>
                <input type="password" class="form-control" ng-model="formValues.ConfirmPassword" id="confirm_password">
                <span class="input-group-addon"><i ng-class="{true: 'fa fa-check green-icon', false: 'fa fa-times red-icon'}[formValues.Password !== '' && formValues.Password === formValues.ConfirmPassword]" aria-hidden="true"></i></span>
              </div>
            </div>
          </div>
          <!-- !confirm-password-input -->
          <!-- admin-checkbox -->
          <div class="form-group" ng-if="isAdmin">
            <div class="col-sm-12">
              <label for="permissions" class="control-label text-left">
                Administrator
                <portainer-tooltip position="bottom" message="Administrators have access to Portainer settings management as well as full control over all defined endpoints and their resources."></portainer-tooltip>
              </label>
              <label class="switch" style="margin-left: 20px;">
                <input type="checkbox" ng-model="formValues.Administrator"><i></i>
              </label>
            </div>
          </div>
          <!-- !admin-checkbox -->
          <!-- teams -->
          <div class="form-group" ng-if="!formValues.Administrator">
            <div class="col-sm-12">
              <label class="control-label text-left">
                Add to team(s)
              </label>
              <span class="small text-muted" style="margin-left: 20px;" ng-if="teams.length === 0">
                You have not yet created any team. Head over the <a ui-sref="teams">teams view</a> to manage user teams.
              </span>
              <span isteven-multi-select
               ng-if="teams.length > 0"
               input-model="teams"
               output-model="formValues.Teams"
               button-label="Name"
               item-label="Name"
               tick-property="ticked"
               helper-elements="filter"
               search-property="Name"
               translation="{nothingSelected: 'Select one or more teams', search: 'Search...'}"
               style="margin-left: 20px;">
               </span>
            </div>
          </div>
          <!-- !teams -->
          <div class="form-group" ng-if="isAdmin && !formValues.Administrator && formValues.Teams.length === 0">
            <div class="col-sm-12">
              <span class="small text-muted">
                Note: non-administrator users with no team do not have access to any endpoint by default. Head over the <a ui-sref="endpoints">endpoints view</a> to manage their accesses.
              </span>
            </div>
          </div>
          <div class="form-group">
            <div class="col-sm-12">
              <button type="button" class="btn btn-primary btn-sm" ng-disabled="!state.validUsername || formValues.Username === '' || formValues.Password === '' || formValues.Password !== formValues.ConfirmPassword" ng-click="addUser()"><i class="fa fa-user-plus" aria-hidden="true"></i> Add user</button>
              <i id="createUserSpinner" class="fa fa-cog fa-spin" style="margin-left: 5px; display: none;"></i>
              <span class="text-danger" ng-if="state.userCreationError" style="margin: 5px;">
                <i class="fa fa-exclamation-circle" aria-hidden="true"></i> {{ state.userCreationError }}
              </span>
            </div>
          </div>
        </form>
      </rd-widget-body>
    </rd-widget>
  </div>
</div>

<div class="row">
  <div class="col-lg-12 col-md-12 col-xs-12">
    <rd-widget>
      <rd-widget-header icon="fa-user" title="Users">
        <div class="pull-right">
          Items per page:
          <select ng-model="state.pagination_count" ng-change="changePaginationCount()">
            <option value="0">All</option>
            <option value="10">10</option>
            <option value="25">25</option>
            <option value="50">50</option>
            <option value="100">100</option>
          </select>
        </div>
      </rd-widget-header>
      <rd-widget-taskbar classes="col-lg-12">
        <div class="pull-left" ng-if="isAdmin">
          <button type="button" class="btn btn-danger" ng-click="removeAction()" ng-disabled="!state.selectedItemCount"><i class="fa fa-trash space-right" aria-hidden="true"></i>Remove</button>
        </div>
        <div class="pull-right">
          <input type="text" id="filter" ng-model="state.filter" placeholder="Filter..." class="form-control input-sm" />
        </div>
      </rd-widget-taskbar>
      <rd-widget-body classes="no-padding">
        <div class="table-responsive">
          <table class="table table-hover">
            <thead>
              <tr>
                <th ng-if="isAdmin">
                  <input type="checkbox" ng-model="allSelected" ng-change="selectItems(allSelected)" />
                </th>
                <th>
                  <a ui-sref="users" ng-click="order('Username')">
                    Name
                    <span ng-show="sortType == 'Username' && !sortReverse" class="glyphicon glyphicon-chevron-down"></span>
                    <span ng-show="sortType == 'Username' && sortReverse" class="glyphicon glyphicon-chevron-up"></span>
                  </a>
                </th>
                <th>
                  <a ui-sref="users" ng-click="order('RoleName')">
                    Role
                    <span ng-show="sortType == 'RoleName' && !sortReverse" class="glyphicon glyphicon-chevron-down"></span>
                    <span ng-show="sortType == 'RoleName' && sortReverse" class="glyphicon glyphicon-chevron-up"></span>
                  </a>
                </th>
                <th ng-if="isAdmin"></th>
              </tr>
            </thead>
            <tbody>
              <tr dir-paginate="user in (state.filteredUsers = (users | filter:state.filter | orderBy:sortType:sortReverse | itemsPerPage: state.pagination_count))">
                <td ng-if="isAdmin"><input type="checkbox" ng-model="user.Checked" ng-change="selectItem(user)" /></td>
                <td>{{ user.Username }}</td>
                <td>
                  <i ng-if="user.Role === 1" class="fa fa-user-circle-o" aria-hidden="true" style="margin-right: 2px;"></i>
                  <i ng-if="user.Role !== 1 && !user.isTeamLeader" class="fa fa-user" aria-hidden="true" style="margin-right: 2px;"></i>
                  <i ng-if="user.isTeamLeader" class="fa fa-user-plus" aria-hidden="true" style="margin-right: 2px;"></i>
                  {{ user.RoleName }}
                </td>
                <td ng-if="isAdmin">
                  <a ui-sref="user({id: user.Id})"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Edit</a>
                </td>
              </tr>
            <tr ng-if="!users">
              <td colspan="4" class="text-center text-muted">Loading...</td>
            </tr>
            <tr ng-if="users.length == 0">
              <td colspan="4" class="text-center text-muted">No users available.</td>
            </tr>
            </tbody>
          </table>
          <div ng-if="users" class="pull-left pagination-controls">
            <dir-pagination-controls></dir-pagination-controls>
          </div>
        </div>
      </rd-widget-body>
    </rd-widget>
  </div>
</div>
